<?php

namespace App\Task\Stat;

use App\Model\AppChannel;
use App\Model\OcpcOppoLog;
use App\Model\StatOcpcOppo;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;

/**
 * 每日oppoocpc统计
 * @Crontab(name="OcpcOppoTask", rule="20 5 * * *", callback="execute", memo="每日oppoocpc统计", singleton=true)
 */
class OcpcOppoTask
{
    public function execute()
    {
        log_info(__CLASS__ . "==Start==oppoocpc统计" . date('Y-m-d H:i:s'));

        $startDate = date('Y-m-d 00:00:00',strtotime('-1 days'));
        $endDate = date('Y-m-d 23:59:59',strtotime('-1 days'));

        $day = date('Y-m-d',strtotime('-1 days'));

        $packages = AppChannel::whereIn('channel',OcpcOppoLog::CHANNELS)->pluck('package_name')->toArray();

        foreach ($packages as $package){
            $model = StatOcpcOppo::query()->where([
                ['day','=',$day],
                ['package_name','=',$package],
            ])->first();

            if(empty($model)){
                $model = new StatOcpcOppo();
            }
            $model->day = $day;
            $model->package_name = $package;
            $model->register_user = OcpcOppoLog::getRegisterUser($startDate,$endDate,$package);
            $model->reg_recharge_user = OcpcOppoLog::getRechargeUser($startDate,$endDate,$package,true);
            $model->reg_recharge_num = OcpcOppoLog::getRechargeNum($startDate,$endDate,$package,true);
            $model->reg_recharge_money = OcpcOppoLog::getRechargeMoney($startDate,$endDate,$package,true);
            $model->recharge_user = OcpcOppoLog::getRechargeUser($startDate,$endDate,$package);
            $model->recharge_money = OcpcOppoLog::getRechargeMoney($startDate,$endDate,$package);
            $model->recharge_num = OcpcOppoLog::getRechargeNum($startDate,$endDate,$package);
            $model->save();
        }
    }
}
